<?php 
class CoreAction extends Action{
	
	var $_Core = array();  //核心数据
	var $topic;  //主题表
	var $post ;  //回复表
	var $class;  //当前所在版块
	var $_topnav;

	function _initialize(){
		$this->topic = D('Topic');
		$this->post  = D('Post');
		$this->_Core['category'] = D('Core')->getCategory();
		
		$this->_Core['isWebAdmin'] = model('UserGroup')->isAdmin($this->mid);
		$this->_Core['myAttention'] = $this->_getLeftAttentionData ();
		$this->assign('Core',$this->_Core);
	}
	
	//版块权限
	protected function boardPurview($isTransact=false){
		$tagId = end( explode(',',$this->class) );
		$Purview['boardInfo'] = D("Board")->where("tagid=".$tagId)->field("tagid,rule,banzhu")->find();
		$Purview['boardInfo']['banzhu'] = explode( ',',$Purview['boardInfo']['banzhu'] );
		$this->assign($Purview);
		$this->_Core['isAdmin'] = ($this->_Core['isWebAdmin'] || in_array($this->mid,$Purview['boardInfo']['banzhu']))?true:false;
		if($isTransact && !$this->_Core['isAdmin']){ 
			$this->error( L('do_no_purview') );
		}
		$this->assign('Core',$this->_Core);
 	}
	
	//获取版块导航
	function getTopNav( $class ,$boardonly=false){
		$catelist = $this->getCategoryTree( $class );
		if( $boardonly ){
			return $catelist;
		}else{
			$topnav[]   = '<a href="'.U('bbs/Index/index').'">'.L('forum').'</a>';
			$topnav     = array_merge($topnav,$catelist);
			$this->_topnav  = $topnav;
			$this->assign( '_topnav', implode( " > ", $topnav) );
		}
	}
	
	function getCategoryTree( $class ){
		$class = explode(',',$class);
		$catelist = $this->_Core['category'];
		return $this->getCate($catelist,$class);
	}
	
	private function getCate($list,$class,$i=0){
		static $arr;
		foreach ($list as $key=>$value){
			if($value['a']==$class[$i] && $class[$i]!=0){
				$arr[] = "<a href='".U('bbs/Index/forumlist',array('class'=>implode(',',array_slice($class,0,$i+1) ) ) )."'>".$value['t']."</a>";
				if($value['d']){
					$this->getCate($value['d'],$class,++$i);
				}
			}
		}
		return $arr;
	}
	
	protected function _getLeftAttentionData($add = true) {
		//获取关注数据
		$attentionData = service ( 'Follow' )->getFollowData ( $uid = $this->mid, $type = 'bbs' );
		
		if (! empty ( $attentionData )) {
			$tagDao = model ( 'Xtag' );
			foreach ( $attentionData as &$value ) {
				$cid = $value;
				$value = array ();
				if (! $add) {
					$title = explode ( ',', $tagDao->getTagName ( $cid ) );
					$value ['title'] = array_pop ( $title );
				} else {
					$value ['title'] = $tagDao->getTagName ( end( explode(',',$cid) ) );
				}
				$value ['cid'] = $cid;
			}
		}
		return $attentionData;
	}	
}
?>